//
//  DoctorInfoController.m
//  KYProject
//
//  Created by Mxxx on 2016/12/22.
//  Copyright © 2016年 Roman. All rights reserved.
//

#import "DoctorInfoController.h"
#import "DoctorTopView.h"
#import "DoctorTextView.h"
#import "NetWorkTool.h"
#import "DefineConstant.h"
#import "NetWorkDefine.h"
@interface DoctorInfoController ()<UIScrollViewDelegate>

@property (nonatomic,weak)DoctorTopView *topView;

@property (nonatomic,weak)UIButton *ConsultButton;

//记录提示框
@property (nonatomic,strong)UIAlertController*alert;
//记录销毁提示框的tap手势
@property (nonatomic,weak)UITapGestureRecognizer *tap;
//记录scrollView
@property (nonatomic ,weak)UIScrollView *scrollView;
//记录中间的按钮视图
@property (nonatomic,weak)DoctorTextView *midView;

@property (nonatomic,weak)UILabel *labelSpecial;

@property (nonatomic,weak)UILabel *leftText;

@property (nonatomic,weak)UILabel *rightText;

@property (nonatomic,assign)BOOL isAttention;

//记录关注成功显示的视图
@property (nonatomic,weak)UIView *OKView;
//记录关注失败显示的视图
@property (nonatomic,weak)UIView *NOView;
@end

@implementation DoctorInfoController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    // Do any additional setup after loading the view.
}

#pragma mark
#pragma mark - 界面搭建
- (void)setupUI
{
#pragma mark - 设置控制器title,背景颜色
    self.title = @"医生基本信息";
    _isAttention = YES;
    self.view.backgroundColor = [UIColor colorWithWhite:0.95 alpha:1];
    [self addLeftButton];
    [self rightButton];
    [self addTopView];
    [self addConsultButton];
    [self addMidView];
    [self getDoctorRequirement];
    [self getDoctorIntroduct];
}
#pragma mark
#pragma mark - 懒加载关注成功显示的视图
- (UIView *)OKView
{
    if (_OKView ==nil) {
        UIView *NOView = [[UIView alloc]init];
        NOView.backgroundColor =kMyColor;
        NOView.alpha = 0;
        NOView.layer.cornerRadius = 10;
        NOView.layer.masksToBounds = YES;
        
        self.NOView = NOView;
        [self.view addSubview:NOView];
        [NOView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(self.view);
            make.size.mas_equalTo(CGSizeMake(120, 80));
        }];
        UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"cuohao"]];
        [NOView addSubview:imageView];
        [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(NOView);
            make.top.equalTo(NOView).offset(10);
            make.size.mas_equalTo(CGSizeMake(30, 30));
        }];
        
        UILabel *lable = [[UILabel alloc]init];
        lable.text = @"取消关注";
        [NOView addSubview:lable];
        [lable mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(NOView);
            make.bottom.equalTo(NOView.mas_bottom).offset(-10);
        }];
        _NOView = NOView;
    }
    return _NOView;
}

- (void)addLeftButton
{
    UIButton *leftBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0,40,40)];
    [leftBtn setImage:[UIImage imageNamed:@"jiantou"] forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(clickLeftButton:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:leftBtn];
}
- (void)clickLeftButton:(UIBarButtonItem *)button
    {
        if (self.backBlock) {
            self.backBlock();
        }
        [self.navigationController popViewControllerAnimated:YES];
    }

#pragma mark
#pragma mark - 导航栏右侧收藏按钮
- (void)rightButton
{
    UIImage *normalImage = [[UIImage imageNamed:@"shixin"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIImage *HighlightedImage = [[UIImage imageNamed:@"kongxin"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    rightButton.frame = CGRectMake(0, 0, normalImage.size.width, normalImage.size.height);
    [rightButton setImage:normalImage forState:UIControlStateNormal];
    [rightButton setImage:HighlightedImage forState:UIControlStateSelected];
    [rightButton addTarget:self action:@selector(clickRightButton:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *button = [[UIBarButtonItem alloc]initWithCustomView:rightButton];
    self.navigationItem.rightBarButtonItem = button;
}

#pragma mark
#pragma mark - 收藏按钮的点击监听事件
- (void)clickRightButton:(UIButton *)rightButton
{
    //切换button的状态,更新button的图片
    rightButton.selected = !rightButton.selected;
    
    NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)
                           lastObject]stringByAppendingPathComponent:@"doctorList.plist"];
    NSArray *arr = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
    NSMutableArray *arrM = arr.mutableCopy;
    if (_isAttention) {
        //取消关注
        [UIView animateWithDuration:0.5 animations:^{
            self.NOView.alpha = 1;
        }
         completion:^(BOOL finished) {
             [UIView animateWithDuration:0.5 animations:^{
                 self.NOView.alpha = 0;
             }];
         }];
        [arrM removeObjectAtIndex:self.indexPath.row];
        [NSKeyedArchiver archiveRootObject:arrM toFile:filePath];
        _isAttention = NO;
    }
    else
    {
        //增加关注
        [UIView animateWithDuration:0.5 animations:^{
            self.OKView.alpha = 1;
        }completion:^(BOOL finished) {
            [UIView animateWithDuration:0.5 animations:^{
                self.OKView.alpha = 0;
            }];
        }];
        [arrM insertObject:self.model atIndex:self.indexPath.row];
        [NSKeyedArchiver archiveRootObject:arrM.copy toFile:filePath];
        
        _isAttention = YES;
    }
}

#pragma mark
#pragma mark - 添加顶部视图

- (void)addTopView
{
    DoctorTopView *topView = [DoctorTopView topView];
    
    [self.view addSubview:topView];
    
    self.topView = topView;
    
    topView.model = self.model;
    
    [topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_topLayoutGuideBottom);
        make.left.trailing.equalTo(self.view);
        make.height.equalTo(@120);
    }];
}

#pragma mark
#pragma mark - 添加底部咨询按钮
- (void)addConsultButton
{
    UIButton *consultButton = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [consultButton setTitle:@"咨询" forState:UIControlStateNormal];
    [consultButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [consultButton setBackgroundImage:[UIImage imageNamed:@"nav"] forState:UIControlStateNormal];
    [self.view addSubview:consultButton];
    self.ConsultButton = consultButton;
    
    [consultButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.view).offset(10);
        make.bottom.trailing.equalTo(self.view).offset(-10);
        make.height.equalTo(@40);
    }];
    //添加按钮点击监听
    [consultButton addTarget:self action:@selector(clickConsultButton:) forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark
#pragma mark - 咨询按钮的点击监听事件
- (void)clickConsultButton:(UIButton *)button
{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"医生关闭了咨询" preferredStyle:UIAlertControllerStyleAlert];
    self.alert = alert;
    
    [self presentViewController:alert animated:YES completion:^{
        [UIView animateWithDuration:3 animations:^{
            
        }completion:^(BOOL finished) {
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW,(int64_t)(2 * NSEC_PER_SEC)),
                           dispatch_get_main_queue(),^{
                               [alert dismissViewControllerAnimated:YES completion:nil];
                           });
        }];
    }];
}
#pragma mark
#pragma mark - button被点击的监听方法
- (void)clickButton:(DoctorTextView *)midView
{
    BOOL isLeft = midView.isLeftButton;
    if (isLeft) {
        [self.scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
    }
    else
    {
        [self.scrollView setContentOffset:CGPointMake([UIScreen mainScreen].bounds.size.width,0) animated:YES];
    }
}

#pragma mark
#pragma mark - 添加中间内容视图
- (void)addMidView
{
    DoctorTextView *midView = [[DoctorTextView alloc]init];
    
    [self.view addSubview:midView];
    
    //属性赋值
    self.midView = midView;
    [midView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.topView.mas_bottom).offset(10);
        make.leading.trailing.equalTo(self.view);
        make.height.equalTo(@48);
    }];
#pragma mark - 注册button被点击的监听通知
    [midView addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventValueChanged];
    
#pragma mark - 添加scrollView
    UIScrollView *scrollView = [[UIScrollView alloc]init];
    [self.view addSubview:scrollView];
    scrollView.backgroundColor = [UIColor redColor];
    [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(midView.mas_bottom);
        make.leading.trailing.equalTo(self.view);
        make.bottom.equalTo(self.view).offset(-60);
    }];
    //设置代理
    scrollView.delegate = self;
    //属性赋值
    self.scrollView = scrollView;
    //设置scrollView
    scrollView.bounces = NO;
    scrollView.pagingEnabled = YES;
    scrollView.showsHorizontalScrollIndicator = NO;
    //将scrollView布局
    [scrollView layoutIfNeeded];
    
    //添加容器视图
    UIView *contentView = [[UIView alloc]init];
    [scrollView addSubview:contentView];
    [contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(scrollView);
        make.size.mas_equalTo(CGSizeMake([UIScreen mainScreen].bounds.size.width * 2,scrollView.bounds.size.height));
    }];
    NSLog(@"%@",contentView);
    //添加左侧View
    UIView *leftView = [[UIView alloc]init];
    leftView.backgroundColor = [UIColor whiteColor];
    [contentView addSubview:leftView];
    [leftView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.leading.bottom.equalTo(contentView);
        make.width.equalTo(scrollView);
    }];
    //添加显示内容的label
    UILabel *labelSpecial = [[UILabel alloc]init];
    labelSpecial.font = [UIFont systemFontOfSize:15];
    labelSpecial.textColor = [UIColor blueColor];
    labelSpecial.numberOfLines = 0;
    [leftView addSubview:labelSpecial];
    
    self.labelSpecial = labelSpecial;
    [labelSpecial mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.leading.equalTo(leftView).offset(20);
        make.trailing.equalTo(leftView).offset(-20);
    }];
    
    UILabel *leftText = [[UILabel alloc]init];
    leftText.font = [UIFont systemFontOfSize:14];
    leftText.numberOfLines = 0;
    [leftView addSubview:leftText];
    
    self.leftText = leftText;
    [leftText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(labelSpecial.mas_bottom);
        make.leading.equalTo(leftView).offset(20);
        make.trailing.equalTo(leftView).offset(-20);
    }];
    
    //添加右侧View
    UIView *rightView = [[UIView alloc]init];
    rightView.backgroundColor = [UIColor whiteColor];
    [contentView addSubview:rightView];
    [rightView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(contentView);
        make.leading.equalTo(leftView.mas_trailing);
        make.width.equalTo(scrollView);
    }];
    //添加显示内容的label
    UILabel *rightText = [[UILabel alloc]init];
    rightText.font = [UIFont systemFontOfSize:14];
    rightText.numberOfLines = 0;
    [rightView addSubview:rightText];
    
    self.rightText = rightText;
    [rightText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.leading.equalTo(rightView).offset(20);
        make.trailing.equalTo(rightView).offset(-20);
    }];
}

#pragma mark
#pragma mark - scrollView的代理方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat offsetX = scrollView.contentOffset.x;
   //self.midView.offsetX = offsetX;
    self.midView.offsetX = offsetX;
}

#pragma mark
#pragma maek - 获取接诊条件的网络数据
- (void)getDoctorRequirement{
    
    NSString *urlString = kDoctorReceivingSetting;
    NSDictionary *paramDic = @{
                               @"doctor_id":@300000315
                               };
    
    [[NetWorkTool shareManager] POST_RequestWithUrlString:urlString parmas:paramDic successBloock:^(NSDictionary *responseObject) {
        //receiving_setting_extra
        //receiving_settings
        NSDictionary *dict = responseObject[@"data"];
        self.labelSpecial.text = dict[@"receiving_setting_extra"];
        
        NSArray *array = dict[@"receiving_settings"];
        NSMutableString *strM = [NSMutableString string];
        for (NSString *str in array) {
            [strM appendFormat:@"%@\n",str];
        }
        self.leftText.text = strM.copy;
        //        NSLog(@"responseObject = %@",array);
    } failureBlock:^(NSError *error) {
        
        NSLog(@"error = %@",error);
    }];
    
}
- (void)getDoctorIntroduct{
    
    NSString *urlString = kGetIntroductionUrl;
    NSDictionary *paramDic = @{
                               @"doctor_id":@300000315
                               };
    
    [[NetWorkTool shareManager] POST_RequestWithUrlString:urlString parmas:paramDic successBloock:^(id responseObject) {
        NSDictionary *dict = responseObject[@"data"];
        self.rightText.text = dict[@"introduction"];
        NSLog(@"responseObject = %@",responseObject);
    } failureBlock:^(NSError *error) {
        
        NSLog(@"error = %@",error);
    }];
    
}


- (void)dealloc{
    
    NSLog(@"医生基本信息页面销毁");
}

@end
